package com.novak.rss.reader;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

public class RSSHelper
{
  private ChannelInfo ci = null;
  private List<NewsItem> items = null;
  private String path;

  public RSSHelper(String path) throws ParserConfigurationException,
      SAXException, IOException
  {
    this.path = path;
    this.parseXML();
  }

  private void parseXML() throws ParserConfigurationException,
      SAXException, IOException
  {
    URL url = null;
    url = new URL(path);
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();
    XMLReader xr = sp.getXMLReader();
    RSSHandler myHandler = new RSSHandler();
    xr.setContentHandler(myHandler);
    xr.parse(new InputSource(url.openStream()));
    items = myHandler.getParsedData();
    ci = myHandler.getRssChannel();

  }

  public ChannelInfo getCi()
  {
    return ci;
  }

  public void setCi(ChannelInfo ci)
  {
    this.ci = ci;
  }

  public List<NewsItem> getItems()
  {
    return items;
  }

  public void setItems(List<NewsItem> items)
  {
    this.items = items;
  }

}
